use "$path\datasets\rd_dataset.dta", clear

local depvar "ch_total_child_places_pc"


preserve
bysort gkz legislative_period partei: keep if _n==1
keep if abs(margin_1)<0.05
keep if elected_women<.
bysort gkz legislative_period: egen count=count(gkz)
bysort gkz legislative_period: egen share_fem_margvictory=mean(elected_women)
bysort gkz legislative_period: egen number_fem_margvictory=sum(elected_women)
gen number_fem_margvictory_ex1= number_fem_margvictory
replace number_fem_margvictory_ex1=number_fem_margvictory-1 if number_fem_margvictory>0 & number_fem_margvictory<.
bysort gkz legislative_period: keep if _n==1
keep  gkz legislative_period number_fem_margvictory number_fem_margvictory_ex1 share_fem_margvictory count
sort gkz legislative_period
tempfile temp
save `temp', replace
restore

merge n:1 gkz legislative_period   using `temp'



gen sum_elected_female_ex_last=sum_elected_female
bysort gkz jahr: egen sum_women=sum(elected_women)
replace sum_elected_female_ex_last=sum_elected_female_ex_last-1 if elected_women==1
gen noothertherwomen=(number_fem_margvictory_ex1==0) if number_fem_margvictory_ex1<.


* keep only municipalities with more than one close mixed-gender race 
keep if count>=2 & count<.
unique gkz legislative_period


preserve
keep if  number_fem_margvictory_ex1==0 

bandwidth_and_weights, depvar(`depvar') var(margin_1)  bwmethod(CCT) kernel(tri) degree(1) number_of_elec(num_of_obs)
ivreg2 `depvar' elected_women margin_1 inter_1   if abs(margin_1)<$bw_opt  [pw=weight] , r cluster(gkz ) partial(margin_1 inter_1 )
est store m1
estadd local bw "CCT"
estadd local degree "Linear"
estadd local bw_length  $bw_opt
unique gkz_legis_party if e(sample)
estadd local num_of_elections  `"`r(sum)'"'
sum `depvar' if e(sample)
estadd scalar  mean_depvar =r(mean)
estadd scalar sd_depvar =r(sd)
restore



preserve
keep if  number_fem_margvictory_ex1>=1 & number_fem_margvictory_ex1<.

bandwidth_and_weights, depvar(`depvar') var(margin_1)  bwmethod(CCT) kernel(tri) degree(1) number_of_elec(num_of_obs)
ivreg2 `depvar' elected_women margin_1 inter_1   if abs(margin_1)<$bw_opt  [pw=weight] , r cluster(gkz ) partial(margin_1 inter_1 )
est store m2
estadd local bw "CCT"
estadd local degree "Linear"
estadd local bw_length  $bw_opt
unique gkz_legis_party if e(sample)
estadd local num_of_elections  `"`r(sum)'"'
sum `depvar' if e(sample)
estadd scalar  mean_depvar =r(mean)
estadd scalar sd_depvar =r(sd)
restore



preserve


gen base_effect=number_fem_margvictory_ex1
gen base_effect_elected_women = base_effect*elected_women
gen base_effect_margin_1=base_effect*margin_1
gen base_effect_inter_1=base_effect*margin_1*elected_women



bandwidth_and_weights, depvar(`depvar') var(margin_1)  bwmethod(CCT) kernel(tri) degree(1) number_of_elec(num_of_obs)

ivreg2 `depvar' elected_women base_effect base_effect_elected_women  margin_1 inter_1  base_effect_margin_1 base_effect_inter_1  if abs(margin_1)<$bw_opt  [pw=weight] , r cluster(gkz ) partial(margin_1 inter_1 base_effect_margin_1 base_effect_inter_1)
est store m3
estadd local bw "CCT"
estadd local degree "Linear"
estadd local bw_length  $bw_opt
unique gkz_legis_party if e(sample)
estadd local num_of_elections  `"`r(sum)'"'
sum `depvar' if e(sample)
estadd scalar  mean_depvar =r(mean)
estadd scalar sd_depvar =r(sd)
restore



************************************************************************************************************************************



esttab   m1 m2 m3 using TableA17/tableA17.txt, style(tab) replace order( ) mlabel(,none) ///
cells(b(label(coef.) star fmt(%8.3f) ) se(label((z)) par fmt(%6.3f))) ///
collabels(none) ///
keep (elected_women base_effect base_effect_elected_women ) ///
stats(bw bw_length degree N   N_clust mean_depvar sd_depvar , layout( @ @ @ @ @  `""@ (@)""' )  fmt( %~#s %9.2f %~# %9.0g %9.0g  %9.2f %9.2f  ) ///
labels("Bandwidth type" "Bandwidth size" "Polynomial"   "N"  "Municipalities" "Mean (SD)"  )) ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
varlabels( elected_women "Female victory" base_effect "Other women" base_effect_elected_women "Female $\times$ Other women"  )




